package com.ld.security.shop.crawler.modules.re.crawler.dd;

import com.ld.security.shop.crawler.modules.re.constant.CrawlerConstant;
import com.ld.security.shop.crawler.modules.re.crawler.BaseCrawler;
import com.ld.security.shop.crawler.modules.re.entity.ReDetails;
import com.ld.security.shop.crawler.modules.re.entity.RePage;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class DdRePageCrawler extends BaseCrawler<RePage> {


    @Autowired
    DdReDetailsCrawler ddReDetailsCrawler;

    public DdRePageCrawler() {
        super(CrawlerConstant.projectCode.dd);
    }

    @Override
    public RePage dataCrawler(String url) {
        RePage page = super.dataCrawler(url);

        Document document =  this.parse(page.getContent());
        Element content =  document.select(".shoplist").first();

        page.setContent(content.html());

        Elements lis =  content.select("li");

        for (Element li : lis) {
            Element a =  li.select("a").first();
            String href = a.attr("href");
            ReDetails reDetails =  ddReDetailsCrawler.dataCrawler(href);
            iReDetailsService.save(reDetails);
            ddReDetailsCrawler.parsing(reDetails);
        }

        return page;
    }




}

